#Python's variables and primitive types
A computer is a machine for calculation, and a program is the steps of calculation. The function of a programming language is to describe the steps of calculation. Therefore, the variable
used to save values is the most basic concept in programming.
A variable, as the name implies, is a variable that can be changed. If the value of a variable does not change, it is called a constant
.
The format for creating a variable is variable name: variable type = variable initial value
, for example:
age_of_yukari:int = 17 # Create a variable named age_of_yukari of type integer(int) with an initial value of 17
sex_of_yukari:str = "female" # Create a variable named sex_of_yukari of type string (str) with an initial value of "female"
age_of_yukari
andsex_of_yukari
are the names of the variables.int
andstr
are the types of variables.17
and"female"
are the values of the variable. This kind of directly written value is called a literal.
#Naming rules
Variable names can only consist of letters, numbers, and underscores, and cannot start with a number.
In addition, there are some commonly accepted naming conventions. Refer to PEP 8
- Names should explain what the variable does, e.g.
age_of_yukari
for Yukari's age. - Variable and function names should use lowercase snake case with underscores separating words, e.g.
age_of_yukari
. - Constant names should use uppercase snake case with underscores separating words, e.g.
AGE_OF_YUKARI
. - Class names should use uppercase camel case with uppercase letters separating words and starting with an uppercase letter, e.g.
AgeOfYukari
. - Private variables should start with two underscores, e.g.
__age_of_yukari
. - Names with two leading and trailing underscores are strictly prohibited and are used for special purposes, e.g.
__age_of_yukari__
. - Special variables with one and only one underscore,
_
, are used to accept values that are not needed.
#Basic types in Python
In Python there are the following primitive types:
Type | Name | Description |
---|---|---|
int | Integer | Used to store integers, such as: 0 , -1 , 233 |
float | Floating point | Used to store decimals, such as: 3.14 , 2.71828 |
str | String | Used to store text, such as: "hello world" |
bool | Boolean | Used to represent logical truth or falsehood, only two values: True and False |
None | Empty value | Used to represent non-existent objects, only one value: None |
#int
Integer variables are used to store integers, such as: 0
, -1
, 233
.
Computers use binary to store data, so some low-level development often uses binary, octal and hexadecimal to represent values.
In Python, the 0x
prefix is used to represent hexadecimal literals, such as: 0xff
, 0x1234abcd
.
In Python, 0o
is used to represent octal, such as: 0o23
, 0o456
.
In Python, 0b
is used to represent binary, such as: 0b0011
, 0b1110
.
If you don't know what binary and hexadecimal are, please check the General Basics - Hexadecimal page.
For very large integers, such as 1000000000000
, it can be hard to quickly tell how many zeros there are, so Python allows you to use underscores (_
) to separate integer literals.
So 1000000000000
can be written as 1_000_000_000_000
or 1_0000_0000_0000
.
#float
Floating-point variables are used to store decimals, for example: 3.14
, 2.71828
.
In mathematics, integers are special forms of decimals, so integer literals can also be assigned to floating-point variables.
Floating-point literals can be expressed in scientific notation, using e
to represent ×10^
. For example, 2.33e8
represents 2.33 multiplied by 10 to the 8th power
It is called floating-point because in the format of computer storage of decimals, the number of digits used to store the integer part and the number of digits used to store the decimal part are variable, that is, the position of the decimal point is floating. Correspondingly, a format in which the number of digits used to store the integer part and the number of digits used to store the decimal part are immutable is called a fixed-point number.
#str
String variables are used to store text, such as "hello world"
.
The contents of a string literal are enclosed in pairs of single quotes ('
) or double quotes ("
).
If the contents of a string contain single quotes ('
), they are usually enclosed in double quotes ("
).
If the contents of a string contain double quotes ("
), they are usually enclosed in single quotes ('
).
If the contents of a string contain both single quotes ('
) and double quotes ("
), they need to be escaped with a backslash (\
).
Some examples:
"I'm robot" # Use double quotes to wrap the content, and the content contains single quotes
"I say \"hello world\"" # Use double quotes to wrap the content, and use backslash to escape double quotes.
#Escape character
Escape characters are used to represent characters that cannot be represented directly, such as \n
for line breaks. The following is a table of escape characters:
Escape character | Illustrate |
---|---|
\\ | Represents a backslash (\ ) itself |
\' | Represents a single quote (' ) |
\" | Represents a double quote (" ) |
\b | Backspace |
\n | newline character |
\v | Vertical tab |
\t | tab |
\r | Enter(Return) |
\f | Page change |
\oNN | Octal byte, for example, \o12 means line break |
\xNN | Hexadecimal byte, for example, \x0a represents a newline character |
#Index
The string is followed by square brackets ([]
) to access characters. Like most programming languages, Python's index starts at 0:
text:str = "ABCDEFG"
print(text[4])
#bool
Boolean type is used to represent the result of logical operation, and has only two values: true (True
) and false (False
).
Boolean values can be operated by and
, or
, and not
.
and
means and, when multiple Boolean values are ANDed, the result isTrue
only when all areTrue
, otherwise the result isFalse
.or
means or, when multiple Boolean values are ORed, the result isFalse
only when all areFalse
, otherwise the result isTrue
.not
means not, inverting a Boolean value.
print("not True is", not True)
print("not False is", not False)
print("True and True is", True and True)
print("True and False is", True and False)
print("False and True is", False and True)
print("False and False is", False and False)
print("True or True is", True or True)
print("True or False is", True or False)
print("False or True is", False or True)
print("False or False is", False or False)
#None
The None
value indicates a non-existent object, which will not be explained in detail here.
#Type hints
Looking back at the code at the beginning, :int
and :str
are called type hints, refer to PEP 484.
age_of_yukari:int = 17 # Create a variable named age_of_yukari of type integer(int) with an initial value of 17
sex_of_yukari:str = "female" # Create a variable named sex_of_yukari of type string (str) with an initial value of "female"
Python is a dynamically typed language, and type hints are only used as hints to increase the readability and maintainability of the code.
Type hints is not required, the above code can be written as:
age_of_yukari = 17 # Create a variable named age_of_yukari of type integer(int) with an initial value of 17
sex_of_yukari = "female" # Create a variable named sex_of_yukari of type string (str) with an initial value of "female"
Obvious types like this don't need to be annotated, and syntax tools can infer the types automatically.